<template>
  <pageTool :isSearch="true" :isadd="true" :alertTitle="`共${alertTitle}条`">
    <template #left>
      <div class="left">
        <el-input style="padding-right: 10px;" v-model="input" placeholder="请输入内容"></el-input>
        <el-button size="mini" @click="onEmpty"> 清除 </el-button>
        <el-button size="mini" type="primary" @click="onSearch" >搜索</el-button>
      </div>
    </template>
    <template #default>
      <el-button type="success" class="el-icon-edit" @click="onAddPermission">  新增权限组</el-button>
    </template>
    <template #conter>
      <el-table
        :data="permissiontableData"
        ref="multipleTable"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading="loading"
        >
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="title"
          label="用户名"
          min-width="30%"
          align="center">
        </el-table-column>
        <el-table-column
          prop="create_date"
          label="日期"
          min-width="40%"
          align="center">
          <template #default="{row}">
            {{row.create_date | timeFilters}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          show-overflow-tooltip
          align="center"
          min-width="30%">
          <template #default="{row}">
            <el-button type="primary" icon="el-icon-edit" circle @click="onEdit(row.id)"></el-button>
            <el-button type="success" icon="el-icon-delete" :loading="loading" circle @click="onDelete(row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align:right;"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pagesize"
        layout=" prev, pager, next, jumper, sizes"
        :total="alertTitle">
      </el-pagination>
      <permissionDialog :visible.sync="dialogVisible" :differenttitle="differenttitle" :selectId="selectId" @refresh="PermissiontList()"/>
    </template>
  </pageTool>
</template>

<script>
import permissionDialog from './components/permissionDialog'
import pageTool from '@/utils/pageTool'
import { GetPermissiontList, deletePermissiont } from '@/api/permission'
export default {
  name: 'permissionsPage',
  components: {
    pageTool,
    permissionDialog
  },
  data () {
    return {
      permissiontableData: [],
      query: {
        page: 1,
        pagesize: 10
      },
      input: '',
      dialogVisible: false,
      differenttitle: false,
      alertTitle: 0,
      loading: false,
      selectId: 0,
      selecttitle: '',
      loading: false
    }
  },
  created () {
    this.PermissiontList()
  },
  methods: {
    async PermissiontList (title) {
      this.loading = true
      const { list, counts } = await GetPermissiontList({...this.query, title: title})
      this.permissiontableData = list
      this.alertTitle = counts
      this.loading = false
    },
    // 添加
    onAddPermission () {
      this.differenttitle = false
      this.dialogVisible = true
    },
    // 编辑
    onEdit (id) {
      this.selectId = id
      this.differenttitle = true
      this.dialogVisible = true
    },
    // 分页
    handleSizeChange(val) {
      this.query.pagesize = val
      this.PermissiontList()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.PermissiontList()
    },
    // 删除
    onDelete (id) {
      this.$confirm('此操作将永久删除用户 , 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await deletePermissiont(id)
          this.$message({
            type: 'success',
            message: '已成功删除!'
          });
          this.PermissiontList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })



    },
    // 清空搜索框
    onEmpty () {
      this.input = ''
      this.PermissiontList()
    },
    // 搜索
    onSearch () {
      const title = this.selecttitle = this.input
      this.PermissiontList(title)
    }
  }
}
</script>

<style lang="scss" scoped>
.left{
    display: flex;
    justify-content: baseline;
  }
</style>